import requests,sys,time
from fake_useragent import UserAgent
from billiard.pool import Pool

ua=UserAgent()

def getDomain(parent,subDomains,timeout):
  try:
    res=requests.get("http://{}.{}".format(subDomains,parent),timeout=int(timeout),headers={"User-Agent":ua.random})
    if(res.status_code==200):
      return f"{subDomains}.{parent}"
  except:
    pass
  
def getArgs():
  args=sys.argv
  args.pop(0)
  newArg={}
  i=1
  while i<len(args):
    newArg[args[i-1]]=args[i]
    i+=2
  return newArg

def main(domain,pro,timeout):
  p=Pool(int(pro))
  with open("./dict.txt") as file:
    fileRead=file.read().splitlines()
    result=[]
    for subdomain in fileRead:
      res=p.apply_async(getDomain,args=(domain,subdomain,timeout))
      if res.get()!=None:
        result.append(res.get())
    p.close()
    p.join()
    if len(result)==0:
      return ["http://{}".format(domain)]
    p=Pool(5)
    result1=[]
    for subdomain in result:
      result1.append(p.apply_async(main,args=(subdomain,pro,timeout)))
    p.close()
    p.join()
    newres=[domain]
    for res in result1:
      newres.extend(res.get())
    return newres

if __name__ =="__main__":
  args=getArgs()
  processLen=args["-p"] if args.get("-p") else 5
  timeout=args["-t"] if args.get("-t") else 10000
  result=main(args["-d"],processLen,timeout)
  result.pop(0)
  print("共{}子域名".format(len(result)))
  file=open('./log/{}.txt'.format(time.strftime("%Y-%m-%d %H:%M:%S")),"a+")
  for res in result:
    print(res)
    file.write(res+"\n")
